<?php
apf_require_class("Solr_Dao_Base");
class Solr_Dao_Report extends Solr_Dao_Base{

    public function insert_report($params) {
        $pdo = $this->get_srm_pdo();
        $sql = "insert into report set hostname_id=?,webapp_id=?,application_id=?,datei=?,doc_number=?,doc_number_time=?,
        					count_min=?,slow_min=?,max_count_min=?";
        $stmt = $pdo->prepare($sql);
        $stmt->execute(array($params['hostname_id'],$params['webapp_id'],$params['application_id'],$params['datei'],$params['doc_number'],$params['doc_number_time'],
        $params['count_min'],$params['slow_min'],$params['max_count_min']));
        return $stmt->rowCount();
    }

    public function update_report($params,$app_id,$datei){
        $pdo = $this->get_srm_pdo();
        $set_str=array();
        if(isset($params['doc_number']))$set_str[]="doc_number='{$params['doc_number']}'";
        if(isset($params['doc_number_time']))$set_str[]="doc_number_time='{$params['doc_number_time']}'";
        if(isset($params['count_min']))$set_str[]="count_min='{$params['count_min']}'";
        if(isset($params['slow_min']))$set_str[]="slow_min='{$params['slow_min']}'";
        if(isset($params['max_count_min']))$set_str[]="max_count_min='{$params['max_count_min']}'";
        $set_str=implode(',',$set_str);
        $sql = "update report set ".$set_str." where application_id=? and datei=?";
        $stmt = $pdo->prepare($sql);
        $stmt->execute(array($app_id,$datei));
        return $stmt->rowCount();
    }

    public function get_report_info($app_id,$datei){
        $pdo = $this->get_srs_pdo();
        $sql="select * from report where application_id=? and datei=?";
        $stmt = $pdo->prepare($sql);
        $stmt->execute(array($app_id,$datei));
        return $stmt->fetch();
    }

    public function get_reports_list($params,$page,$limit=20){
        $pdo = $this->get_srs_pdo();
        $offset=($page-1)*$limit;
        $where='';
        if($params['hostname_id'])$where.=" and hostname_id='".$params['hostname_id']."' ";
        if($params['webapp_id'])$where.=" and webapp_id='".$params['webapp_id']."' ";
        if($params['datei'])$where.=" and datei='".$params['datei']."' ";
        $sql="select * from report where 1 ".$where." order by datei desc limit $offset,$limit";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function get_reports_count($params){
        $pdo = $this->get_srs_pdo();
        $where='';
        if($params['hostname_id'])$where.=" and hostname_id='".$params['hostname_id']."' ";
        if($params['webapp_id'])$where.=" and webapp_id='".$params['webapp_id']."' ";
        if($params['datei'])$where.=" and datei='".$params['datei']."' ";
        $sql="select count('x') from report where 1 ".$where;
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchColumn();
    }

}